package com.cmd.user;


import com.alibaba.fastjson.JSONObject;
import com.mgr.UserOrderMgr;
import com.player.User;
import com.qy.bean.OrderInfo;
import com.qy.mgr.CheckStationMgr;
import com.qy.util.StaticData;
import com.util.AbstractCommand;
import com.util.PBMessage;

/**
 * 
 * 用户生成订单
 * @author zwl
 * 2018年2月1日
 */
public class OrderCreateCMD  extends AbstractCommand{

	@Override
	public void execute(User user, PBMessage packet) throws Exception {
		/*
		 * 获取手机号
		 * 获取代金券的id
		 * 如果id的userId匹配则更新用户的代金券发送的手机号
		 */
		JSONObject reJs = new JSONObject();
		reJs.put(StaticData.reKey.cmd.getKey(),packet.getCode());
		JSONObject param = packet.getMessage();
		if(!param.containsKey("fetchDate")||!param.containsKey("fetchLocation")||
			!param.containsKey("fetchUserName")||!param.containsKey("returnUserName")||
			!param.containsKey("fetchCoord")||!param.containsKey("fetchPhone")||
			!param.containsKey("returnDate")||!param.containsKey("returnLocation")||
			!param.containsKey("returnCoord")||!param.containsKey("returnPhone")||
			!param.containsKey("plateNumber")||!param.containsKey("vln")){
			reJs.put(StaticData.reKey.code.getKey(), StaticData.codeType.param_error.getType());
			user.sendPacket(reJs);
			return;
		}
		//取车时间
		String fetchDate = param.getString("fetchDate");
		//取车位置
		String fetchLocation = param.getString("fetchLocation");
		//取车坐标
		String fetchCoord = param.getString("fetchCoord");
		//联系电话
		String fetchPhone = param.getString("fetchPhone");
		//取车人称呼
		String fetchUserName = param.getString("fetchUserName");
		//还车时间
		String returnDate = param.getString("returnDate");
		//还车位置
		String returnLocation = param.getString("returnLocation");
		//还车坐标
		String returnCoord = param.getString("returnCoord");
		//联系电话
		String returnPhone = param.getString("returnPhone");
		//还车人称呼
		String returnUserName = param.getString("returnUserName");
		//车牌号
		String plateNumber = param.getString("plateNumber");
		//后6位唯一码
		String vln = param.getString("vln");
		//渠道id
		int channelId = StaticData.no_param;
		if(param.containsKey("channelId")){
		    channelId = param.getInteger("channelId");
		}
		//检车站坐标
		int stationId = CheckStationMgr.getNearest(fetchCoord);
		
		OrderInfo info = UserOrderMgr.createOrder(user.getID(), fetchUserName, fetchPhone, fetchDate, fetchLocation, 
			fetchCoord, returnUserName, returnPhone, returnDate, returnLocation, returnCoord,
			plateNumber, vln, channelId,stationId);
		if(info==null){
			reJs.put(StaticData.reKey.code.getKey(), StaticData.codeType.other_error.getType());
			user.sendPacket(reJs);
			return;
		}
		
		reJs.put("orderId", info.getId());
		reJs.put(StaticData.reKey.code.getKey(), StaticData.codeType.ok.getType());
		reJs.put(StaticData.reKey.cmd.getKey(),packet.getCode());
		user.sendPacket(reJs);
		return;
	}

}
